package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.core.domain.entity.SysUser;
import com.common.utils.SecurityUtils;
import com.common.utils.StringUtils;
import com.project.domain.bo.ProjectTabListBo;
import com.project.domain.bo.VProjectDocumentBo;
import com.project.domain.entity.xjProject.BizXjProjectDocument;
import com.project.domain.entity.zxProject.BizZxProjectDocument;
import com.project.mapper.ProjectDocumentMapper;
import com.project.mapper.xjProject.XjProjectDocumentMapper;
import com.project.mapper.zxProject.ZxProjectDocumentMapper;
import com.project.service.IProjectDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ProjectDocumentServiceImpl implements IProjectDocumentService {

    @Autowired
    ProjectDocumentMapper documentMapper;

    @Autowired
    XjProjectDocumentMapper xjProjectDocumentMapper;

    @Autowired
    ZxProjectDocumentMapper zxProjectDocumentMapper;

    /**
     * 获取文件列表（校级、纵向）
     * @param fileBo
     * @return
     */
    public List<BizXjProjectDocument> getFileList(VProjectDocumentBo fileBo){
        return documentMapper.getFileList(fileBo);
    }

    //region 校级项目文件
    /**
     * 查询校级项目文件
     *
     * @param ID 校级项目文件主键
     * @return 校级项目文件
     */
    @Override
    public BizXjProjectDocument selectBizXjProjectDocumentByID(String ID)
    {
        return xjProjectDocumentMapper.selectById(ID);
    }

    /**
     * 新增校级项目文件
     *
     * @param bizXjProjectDocument 校级项目文件
     * @return 结果
     */
    @Override
    public int insertBizXjProjectDocument(BizXjProjectDocument bizXjProjectDocument)
    {
        return xjProjectDocumentMapper.insert(bizXjProjectDocument);
    }

    /**
     * 修改校级项目文件
     *
     * @param bizXjProjectDocument 校级项目文件
     * @return 结果
     */
    @Override
    public int updateBizXjProjectDocument(BizXjProjectDocument bizXjProjectDocument)
    {
        return xjProjectDocumentMapper.updateById(bizXjProjectDocument);
    }

    /**
     * 批量删除校级项目文件
     *
     * @param IDs 需要删除的校级项目文件主键
     * @return 结果
     */
    @Override
    public int deleteBizXjProjectDocumentByIDs(String[] IDs)
    {
        return xjProjectDocumentMapper.deleteBatchIds(Arrays.asList(IDs));
    }

    /**
     * 删除校级项目文件信息
     *
     * @param ID 校级项目文件主键
     * @return 结果
     */
    @Override
    public int deleteBizXjProjectDocumentByID(String ID)
    {
        return xjProjectDocumentMapper.deleteById(ID);
    }
    //endregion

    /**
     * 保存校级项目文件信息
     * @param projectTabListBo
     * @return
     */
    public String saveXJDocumentInfo(ProjectTabListBo<BizXjProjectDocument> projectTabListBo){
        String res = "保存成功！";
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try{
            String proId = projectTabListBo.getProId(); //项目号
            List<BizXjProjectDocument> xjProjectDocumentList = projectTabListBo.getSlaveTableList();//文件列表
            //不管新建还是编辑都批量删除后新建
            if(StringUtils.isNotBlank(proId)){
                //先删除
                deleteByProId(proId,"xJProject");
                //批量新建
                if(xjProjectDocumentList != null && xjProjectDocumentList.size()>0){
                    for(int m =0;m<xjProjectDocumentList.size();m++){
                        BizXjProjectDocument bizXjProjectDocument = xjProjectDocumentList.get(m);
                        bizXjProjectDocument.setLasteditdate(dateFormat.parse(dateFormat.format(date)));
                        bizXjProjectDocument.setLasteditusername(loginUser.getNickName());
                        bizXjProjectDocument.setLastedituserid(loginUser.getUserRealId());
                        bizXjProjectDocument.setCreatedate(dateFormat.parse(dateFormat.format(date)));
                        bizXjProjectDocument.setCreateuserid(loginUser.getUserRealId());
                        bizXjProjectDocument.setCreateusername(loginUser.getNickName());
                        xjProjectDocumentMapper.insert(bizXjProjectDocument);
                    }
                }
            }
        }catch (Exception exception){
            res = "保存失败！" + exception.getMessage();
        }

        return res;
    }

    /**
     * 保存纵向项目文件信息
     * @param projectTabListBo
     * @return
     */
    public String saveZXDocumentInfo(ProjectTabListBo<BizZxProjectDocument> projectTabListBo){
        String res = "保存成功！";
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            List<BizZxProjectDocument> zxProjectDocumentList = projectTabListBo.getSlaveTableList();
            String proId = projectTabListBo.getProId();
            //不管新建还是编辑都批量删除后新建
            if(StringUtils.isNotBlank(proId)){
                //先删除
                deleteByProId(proId,"zXProject");
                //批量新建
                if(zxProjectDocumentList != null && zxProjectDocumentList.size()>0){
                    for(int m =0;m<zxProjectDocumentList.size();m++){
                        BizZxProjectDocument bizZxProjectDocument = zxProjectDocumentList.get(m);
                        bizZxProjectDocument.setLasteditdate(dateFormat.parse(dateFormat.format(date)));
                        bizZxProjectDocument.setLasteditusername(loginUser.getNickName());
                        bizZxProjectDocument.setLastedituserid(loginUser.getUserRealId());
                        bizZxProjectDocument.setCreatedate(dateFormat.parse(dateFormat.format(date)));
                        bizZxProjectDocument.setCreateuserid(loginUser.getUserRealId());
                        bizZxProjectDocument.setCreateusername(loginUser.getNickName());
                        zxProjectDocumentMapper.insert(bizZxProjectDocument);
                    }
                }
            }
        }catch (Exception exception){
            res = "保存失败！" + exception.getMessage();
        }

        return res;
    }

    /**
     * 根据项目号删除文件信息
     * @param proId
     * @param proClass
     * @return
     */
    public String deleteByProId(String proId,String proClass){
        String res = "删除成功！";
        try{
            if(proClass.equals("xJProject")){
                QueryWrapper<BizXjProjectDocument> xjQueryWrapper = new QueryWrapper<>();
                xjQueryWrapper.eq("PROJECT_ID",proId);
                xjProjectDocumentMapper.delete(xjQueryWrapper);
            }else if(proClass.equals("zXProject")){
                QueryWrapper<BizZxProjectDocument> zxQueryWrapper = new QueryWrapper<>();
                zxQueryWrapper.eq("PROJECT_ID",proId);
                zxProjectDocumentMapper.delete(zxQueryWrapper);
            }else{
                //横向删除
            }
        }catch (Exception exception){
            res = "操作失败！"+ exception.getMessage();
        }

        return res;
    }
}
